﻿using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using VIT.DataHelper.DataAccess;

namespace VIT.Business
{
    public class File : File_Base
    {
        SQLSupport sqlSupport = new SQLSupport();
        #region Constructor
        public File(string Id = "",
                        string FolderId = "",
                        string LanguageId = "",
                        string Name = "",
                        int Size = 0,
                        string Title = "",
                        string Description = "")
        {
            this.Id = Id;
            this.FolderId = FolderId;
            this.LanguageId = LanguageId;
            this.Name = Name;
            this.Size = Size;
            this.Title = Title;
            this.Description = Description;
        }
        #endregion

        #region List
        public DataTable GetList(string folderId="")
        {
            string filter = folderId=="" ? "" : " and FolderId = @FolderId";
            string strQuery = "select Id, FolderId, Name, Size, Title, Description from Files, File_Language where Id=FileId" +
                        " and LanguageID=@LanguageID " + filter + " where Published=1 order by Orders";
            SqlParameter LanguageId = new SqlParameter("@LanguageID", this.LanguageId);
            SqlParameter FolderId = new SqlParameter("@FolderId", folderId);
            return sqlSupport.ExecuteDataset(false, strQuery, LanguageId).Tables[0];
        }

        public void GetSingle()
        {            
            string strQuery = "select Id, FolderId, Name, Size, Title, Description from Files, File_Language " + 
                        "where Id=FileId and LanguageID=@LanguageId and Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            DataTable dt = sqlSupport.ExecuteDataset(false, strQuery, Id, LanguageId).Tables[0];

            if (dt.Rows.Count > 0)
            {
                this.FolderId = dt.Rows[0]["FolderId"].ToString();
                this.Name = dt.Rows[0]["Name"].ToString();
                this.Title = dt.Rows[0]["Title"].ToString();
                this.Description = dt.Rows[0]["Description"].ToString();
                if (!string.IsNullOrEmpty(dt.Rows[0]["Size"].ToString()))
                    this.Size = int.Parse(dt.Rows[0]["Size"].ToString());
            }
            else this.Id = "";
        }
        #endregion
    }
}